package com.example.customcomwidget;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListPopupWindow;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Toast;

import java.util.LinkedList;
import java.util.List;

/**
 * @author hdz
 * @date 2021/08/05 14:30:50
 */
public class MainActivity extends AppCompatActivity implements ListPopupWindowUtil.OnVisibleChangeListener {

    private SelectTextSettingsItem size;
    private SelectTextSettingsItem frame;

    private InputSettingsItem name1;
    private InputSettingsItem name2;

    static {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        name2 = findViewById(R.id.name2);

        name1 = findViewById(R.id.name1);
        name1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                Log.d("TAG", "beforeTextChanged: " + s);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                name2.setText(s.toString());
                Log.d("TAG", "onTextChanged: " + s);
            }

            @Override
            public void afterTextChanged(Editable s) {
                Log.d("TAG", "afterTextChanged: " + s);
            }
        });

        final List<OptionText> data0 = new LinkedList<>();
        data0.add(new OptionText(0, "1920x1080"));
        data0.add(new OptionText(1, "1920x720"));
        data0.add(new OptionText(2, "1280x720"));
        final OptionTextAdapter sizeAdapter = new OptionTextAdapter(this, data0);
        size = findViewById(R.id.size);

        size.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sizeAdapter.setCurIndex(size.getCurIndex());
                if (size.getListPopupWindowUtil() == null) {
                    size.setListPopupWindowUtil(new ListPopupWindowUtil.Builder(v, sizeAdapter)
                            .setTouchX((int) size.touchX)
                            .setBackgroundDrawable(getResources().getDrawable(R.drawable.list_popup_window_bg))
                            .setOnVisibleChangeListener(MainActivity.this)
                            .setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                    OptionText obj = (OptionText) (parent.getAdapter()).getItem(position);
                                    size.setText(obj.getText(), position);
                                }
                            }).builder());
                }
                size.getListPopupWindowUtil().show((int) size.touchX);
                Log.d("TAG", "onClick: " + v.getId() + "=" + R.id.size);
            }
        });

        final List<OptionText> data = new LinkedList<>();
        data.add(new OptionText(0, "60fps"));
        data.add(new OptionText(1, "30fps"));
        data.add(new OptionText(2, "24fps"));
        final OptionTextAdapter adapter = new OptionTextAdapter(this, data);

        frame = findViewById(R.id.frame);
        frame.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                adapter.setCurIndex(frame.getCurIndex());
                new ListPopupWindowUtil.Builder(v, adapter)
                        .setTouchX((int) frame.touchX)
                        .setBackgroundDrawable(getResources().getDrawable(R.drawable.list_popup_window_bg))
                        .setOnVisibleChangeListener(MainActivity.this)
                        .setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                OptionText obj = (OptionText)(parent.getAdapter()).getItem(position);
                                ((SelectTextSettingsItem)v).setText(obj.getText(), position);
                            }
                        })
                        .builder().show((int) frame.touchX);
                Log.d("TAG", "onClick: " + v.getId() + "=" + R.id.frame);
            }
        });

        findViewById(R.id.next).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(), "下一页", Toast.LENGTH_SHORT).show();
            }
        });
    }


    @Override
    public void onVisibleChanged(boolean visible) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        if (visible) {
            lp.alpha = .5f;
        } else {
            lp.alpha = 1.0f;
        }
        getWindow().setAttributes(lp);
    }
}